package com.zhiyou.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
	public JDBCUtils jdb = new JDBCUtils();
	public Connection con = null;
	public PreparedStatement past = null;
	public ResultSet rs = null;
	public  ResultSet select(String sql ,Object obj[]){
		try {
			con = jdb.getConnection();
			past =con.prepareStatement(sql);
			int i = 1;
			while(i<=obj.length){
				past.setObject(i, obj[i-1]);
				i ++;
			}
			rs = past.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;	
	}
	
	public  int update(String sql,Object obj[]){
		int a = 0;
		try {
			con = jdb.getConnection();
			past =con.prepareStatement(sql);
			int i = 1;
			while(i<=obj.length){
				past.setObject(i, obj[i-1]);
				i++;
			}
			a = past.executeUpdate();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			return 0;
		}catch (Exception e) {
			// TODO: handle exception
			return 0;
			
		}
		return a;
		
	}
	
	public int[] dels(String sql ,int id[]){
		int id1[] = null;
		try {
			con = jdb.getConnection();
			past =con.prepareStatement(sql);
			for(int i = 0;i<id.length;i++){
				past.setInt(1, id[i]);
				past.addBatch();
			}
			id1 = past.executeBatch();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return id1;
		
	}
	
	public  void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(past!=null){
			try {
				past.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
}
